<?php
namespace Admin\Controller;
use Think\Controller;
class ActivityController extends Controller {
    public function activities(){

        $activitiesModel=M('activities');//构建一个操作notices表的模型实例
        $activities=$activitiesModel->order('a_id ')->select();
        $this->activities=$activities;
        $activitiesModel = M('activities');
        $qrcodenum = $activitiesModel->order('a_id ')->count();
        $Page = new \Think\Page($qrcodenum, 3);
        $Page->setConfig('header', '共<b>%TOTAL_ROW%</b>条记录&nbsp;&nbsp;
每页<b>3</b>条&nbsp;&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页');
        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('last', '末页');
        $Page->setConfig('first', '首页');
        $Page->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $this->link = $Page->show();
        $this->activities = $activitiesModel->order("a_id desc")->limit($Page->firstRow, 3)->select();
        $this->display();
       

    }
    public function delActivity($a_id){
       // $id=$_GET['id'];
    $activitiesmodel=M('activities'); 
    //where实在构建条件
    if (false!==$activitiesmodel->where("a_id=$a_id"/*必须是双引号*/)->delete()) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败');
        }
        
    }
    public function addActivity(){
    if(IS_POST)
    {
      
      $data['a_title'] = I('post.a_title');
      $data['a_content'] = I('post.a_content');
      $data['a_date'] = I('post.a_date');
      $data['a_tel'] = I('post.a_tel');
      $data['a_cost'] = I('post.a_cost');
      $data['a_address'] = I('post.a_address');
      
      
      // if(empty($data['m_name']))
      // {
      //   $this->error('用户名必须填写');
      // }
      // elseif(empty($password))
      // {
      //   $this->error('密码必须填写');
      // }
      // else
      // {
      //   $data['m_password'] = md5(md5($password) );
        
        $activity = M('activities');
        if($_FILES['file']['error']==0 ){
            $info=$this->upload();
            $name1=$info["file1"]["savename"];
            $name2=$info["file2"]["savename"];
        }
        else{
            $name="default.jpg";
        }
        if($activity->create()){
            $activity->a_pic1=$name1; 
            $activity->a_pic2=$name2;
            if(false!==$activity->add())
                      {
                $this->success('添加成功!','activities');
            }
            else{$this->error('添加失败');}
        }else{
            $this->error($activity->getError());
        }
      // }
    }
    else
    { 
      $this->display();
    }

  }
    public function upload(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Public/Upload/'; // 设置附件上传根目录  //  
        $upload->autoSub = false;//关闭子目录，默认true
        $upload->savePath  =     ''; // 设置附件上传(子)目录，默认目录名为年月日
        $info   =   $upload->upload();  // 上传文件 
        if(!$info) {
            $this->error($upload->getError());    
        }
        else{
            return $info;
        }                    
    }


    public  function editActivity($a_id){
        $activityInfos = M('activities');
        $activity=$activityInfos->where("a_id=$a_id")->find();
        $this->activityInfos=$activity;
        $this->display();
    }

    public function  doUpdate($a_id){
        $activityInfos = M('activities');
        $data['a_title'] = trim(I('post.a_title'));
        $data['a_content'] = trim(I('post.a_content'));
        $data['a_date'] = trim(I('post.a_date'));
        $data['a_tel'] = trim(I('post.a_tel'));
        $data['a_cost'] = trim(I('post.a_cost'));
        $data['a_address'] = trim(I('post.a_address'));

        if ($_FILES['file']['error'] == 0) {

                $info = $this->upload();
                $data['a_pic1'] = $info['file1']['savename'];
                $data['a_pic2'] = $info['file2']['savename'];
            } else {
                $data['photo'] = "default.jpg";
            }


        $activity=$activityInfos->where("a_id=$a_id")->save($data);

        
        if(false !== $activity){
            $this->success('修改成功！','activities');
        }else{
            $this->error('修改失败');
        }
    }
}
?>